--#include "data\config\boss\BossWantedConfig.lua" once
--[[
BOSS首杀
]]

--[[
BossWantedRankColumn = 
{	
	{"bossName"},				--0
	{"killId1"},				--1，第1个击杀者Id
	{"killName1"},				--2，第1个击杀者Name
	{"killTime1"},				--3，第1个击杀时间
	{"killId2"},				--4，第1个击杀者Id
	{"killName2"},				--5，第2个击杀者Name
	{"killTime2"},				--6，第2个击杀时间
	{"killId3"},				--7，第3个击杀者Id
	{"killName3"},				--8，第3个击杀者Name
	{"killTime3"},				--9，第3个击杀时间
	{"killId4"},				--10，第4个击杀者Id
	{"killName4"},				--11，第4个击杀者Name
	{"killTime4"},				--12，第4个击杀时间
	{"killId5"},				--13，第5个击杀者Id
	{"killName5"},				--14，第5个击杀者Name
	{"killTime5"},				--15，第5个击杀时间
}
]]

BossWantedRankColumn = 
{	
	{"bossName"},				--0
	{"killData1"},				--1，第1个击杀数据
	{"killData2"},				--2，第2个击杀数据
	{"killData3"},				--3，第3个击杀数据
	{"killData4"},				--4，第4个击杀数据
	{"killData5"},				--5，第5个击杀数据
}
BossWantedJoinRankColumn =       --每个actorId对应的bossId只能出现一次
{             
	{"bossId"},                  --参与玩家记录（首杀记录完成后，将之后的参与者记录到此处）
} 


BossWantedRank = Lang.Rank.BossWantedRank
BossWantedJoinRank = Lang.Rank.BossWantedJoinRank		

function InitBossWantedRank()
	--print("--->InitBossWantedRank...")
	Rank.Init(BossWantedRank, BossWantedRank, BossWantedRankColumn, 1000, 0)
	--Rank.Init(BossWantedJoinRank, BossWantedJoinRank, BossWantedJoinRankColumn, 1000, 0)
end

--[[
初始化悬赏BOSS数据
]]
function InitBossWantedData()
	local count = GetCommonRankCount( BossWantedRank )
	--print("InitBossWantedData, count="..count)
	if count < 5 then
		RefreshBossWantedData()
	end
end

--[[
每日处理一次
]]
function OnBossWantedDailyProc( )
	ClearCommonRank( BossWantedRank )		--清除排行榜
	--ClearCommonRank( BossWantedJoinRank )		--清除排行榜
	RefreshBossWantedData()
end

function RefreshBossWantedData()
	local elem = GetElemByOpenServerDay( BossWantedConfig.bossWanted )
	if not elem then
		return
	end

	local useBoss = DistributeElem( elem.boss, 5, false )
	if useBoss then
		for bossIdx, bossElem in ipairs( useBoss ) do
			SetCommonRankValueById( bossElem.bossId, BossWantedRank, 0 )
			--print("RefreshBossWantedData, bossIdx="..bossIdx..", bossId="..bossElem.bossId)
		end
	end
end



function ClearBossWantedData(  )
	
end

function SetBossWantedKillFlag( sysarg , bossId)
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.bossWantedKillFlags then
		sVar.bossWantedKillFlags = {}
	end
	sVar.bossWantedKillFlags[bossId] = 1
end

function GetBossWantedKillFlag( sysarg , bossId )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.bossWantedKillFlags then
		sVar.bossWantedKillFlags = {}
	end
	return sVar.bossWantedKillFlags[bossId] or 0
end

function OnBossWantgedDailyProc( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.bossWantedKillFlags = nil
end

function OnBossWantedBeKilled( sysarg, bossId )
	local bossIdx = GetBossWantedBossIdx( bossId )
	--print( "OnBossWantedBeKilled, bossId="..bossId..", bossIdx="..bossIdx )
	if bossIdx < 1  or bossIdx > 5 then 			--非悬赏BOSS
		return
	end

	local killIdx = AddtBossWantedKillData( sysarg, bossId )
	if 1 <= killIdx and killIdx <= 5 then				--新的击杀者
		--print(" the actor get kill awards")
		SendBossWantedAwardsByMail( sysarg, bossId ,true)
	elseif killIdx == -1 then							--已经击杀过了
		--print(" the actor has killed the boss")
	else 												--此BOSS击杀次数已满
		--print(" the actor get mini awards")
		--SetBossWantedJoinRankData( sysarg , bossId)      --参与者记录
		SendBossWantedAwardsByMail( sysarg, bossId, false)
	end
	SetBossWantedKillFlag( sysarg , bossId)
	--print("AddtBossWantedKillData, killIdx="..killIdx)
end

function SendBossWantedAwardsByMail( sysarg, bossId, isInPre)
	--print("SendBossWantedAwardsByMail...")
	local actorId = Actor.getActorId( sysarg )
	local elem = GetElemByOpenServerDay( BossWantedConfig.bossWanted )
	if not elem then
		return
	end
	local bossName = System.getMonsterNameById(bossId)   
	local mailTitle = Lang.ScriptTips.BossWantedMailTitle2
	local mailContent =  string.format( Lang.ScriptTips.BossWantedMailContent2, bossName)

	local killAwards = elem.killAwards
	local miniAwards = elem.miniAwards

	local awards = miniAwards[bossId]

	if isInPre then
		awards = killAwards[bossId]
		mailTitle = Lang.ScriptTips.BossWantedMailTitle1
		mailContent = string.format( Lang.ScriptTips.BossWantedMailContent1, bossName )
	end
	--print("actorid= "..actorId.." bossId= "..bossId)
	if actorId > 0 and bossId > 0 and awards then
		--print("mailContent:"..mailContent)
		Awards.GiveToMailById( actorId, awards, mailTitle, mailContent, LogIds.BossWantedAwardsLogId, Lang.LogTips.BossWantedAwards)
	end
end

function GetBossWantedBossIdx( bossId )
	for idx=1,5 do
		if GetCommonRankIdByIdx( idx, BossWantedRank ) == bossId then
			return idx
		end
	end
	return 0
end

--[[
悬赏BOSS增加击杀次数
]]
function AddtBossWantedKillData( sysarg, bossId )
	local actorId = Actor.getActorId( sysarg )
	--print("AddtBossWantedKillData, bossId="..bossId)
	for killIdx=1,5 do
		local killData 	= GetBossWantedKillData( bossId, killIdx )
		local killerId 	= killData[1] or 0
		--print("AddtBossWantedKillData, killIdx="..killIdx..", killerId="..killerId)
		if killerId == actorId then 		--该玩家已经击杀过此BOSS
			return -1
		end

		if killerId <= 0 then
			local actorIdName 	= Actor.getName( sysarg )
			local killTime		= System.getCurrMiniTime()
			local newKillData 	= { actorId, actorIdName, killTime }
			SetBossWantedKillData( bossId, killIdx, newKillData )
			return killIdx 			--成为第N个击杀者
		end
	end
	return 0 		--已经击杀满了（5次）
end

--[[
获取BOSS悬赏的击杀数据
]]
function GetBossWantedKillData( bossId, killIdx )
	local data  = GetCommonRankColumnValueById( bossId, BossWantedRank, killIdx ) or 0
	if data == 0 then
		return {}
	end
	return BaseFuc_unserialize( data )
end

--[[
设置BOSS悬赏的击杀数据
killData = { actorId, actorName, killTime }
killIdx: 1-5
]]
function SetBossWantedKillData( bossId, killIdx, killData )
	local strData = BaseFuc_serialize( killData )
	--print("SetBossWantedKillData, bossId="..bossId..", killIdx="..killIdx..", strData="..strData)
	return SetCommonRankColumnValueById( bossId, BossWantedRank, 0, killIdx, strData )
end


--[[
设置BOSS悬赏参与榜数据
]]
function SetBossWantedJoinRankData( sysarg, bossId )
	local actorId = Actor.getActorId( sysarg )
	if GetCommonRankColumnValueById(actorId, BossWantedJoinRank, 0) ==  bossId then
		--print("SetBossWantedJoinRankData this actorId with bossId is exited")
		return
	end

    SetCommonRankColumnValueById(actorId, BossWantedJoinRank, 0, 0, bossId)
end


function SendBossWantedData( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sSendBossWantedData)
	if npack then
		local count = 5
		DataPack.writeByte( npack, count )
		for bossIdx=1, count do
			local bossId 	= GetCommonRankIdByIdx( bossIdx, BossWantedRank )
			local killCount = 5
			DataPack.writeByte( npack, bossIdx )
			DataPack.writeWord( npack, bossId )	
			local bossKillFlag = GetBossWantedKillFlag( sysarg , bossId )
			DataPack.writeByte( npack, bossKillFlag )	
			DataPack.writeByte( npack, killCount )
			--print("SetBossWantedKillData, bossIdx="..bossIdx..", bossId="..bossId..", killCount="..killCount)
			for killIdx=1, killCount do
				local killData 	= GetBossWantedKillData( bossId, killIdx )
				local killerId  = killData[1] or 0
				local killerName= killData[2] or ""
				DataPack.writeByte( npack, killIdx )
				DataPack.writeUInt( npack, killerId )
				DataPack.writeString( npack, killerName )
				--print("  SetBossWantedKillData, killIdx="..killIdx..", killerId="..killerId..", killerName="..killerName)
			end
		end
		DataPack.flush(npack)
	end
end

--[[
发送奖励给玩家们
]]
function SendBossWantedAwardsToPalyers( )
	local elem = GetElemByOpenServerDay( BossWantedConfig.bossWanted )
	if not elem then
		return
	end
	local killAwards = elem.killAwards
	local miniAwards = elem.miniAwards

	--给前5的玩家发送首杀奖励
	for bossIdx=1, 5 do                                    --五个Boss
		local bossId = GetCommonRankIdByIdx( bossIdx, BossWantedRank ) or 0
		for killIdx=1, 5 do                                --五个首杀记录
			local killData 	= GetBossWantedKillData( bossId, killIdx )
			local killerId 	= killData[1] or 0
			local awards    = killAwards[bossId]
			--print("pre five actor: "..killerId)
			if killerId > 0 then
				Awards.GiveToMailById( killerId, awards, Lang.ScriptTips.BossWantedMailTitle1, Lang.ScriptTips.BossWantedMailContent1, LogIds.BossWantedAwardsLogId, Lang.LogTips.BossWantedAwards)
			end	
		end
	end

	--给非前五的玩家发送悬赏奖励
	local rankPtr = Ranking.getRanking(BossWantedJoinRank)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	--print("rankCount= "..rankCount)
	for rankIdx=1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, rankIdx-1)
		if item then
            local actorId = tonumber(Ranking.getId(item))
            local awards  = miniAwards[tonumber(Ranking.getSub(item, 0))]
            --print("out of pre five actor: "..actorId)
            if actorId > 0 then
				Awards.GiveToMailById( actorId, awards, Lang.ScriptTips.BossWantedMailTitle1, Lang.ScriptTips.BossWantedMailContent1, LogIds.BossWantedAwardsLogId, Lang.LogTips.BossWantedAwards)
			end     
        end
	end
	
	
end

InitBossWantedRank()			
InitBossWantedData()			


